class Admin::TopicController < ApplicationController
  before_filter :authorize,:only=>['new','create','edit','update','destroy']
  layout "admin" ,:except=>['notop']

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @topics_pages=Topic.find(:all,:order => "istop DESC,created_at DESC") 
    @topic_pages, @topics = paginate_collection @topics_pages, {:page => @params[:page],:per_page => 10,:order => "created_at DESC" }
  end
  
  def notop
  @topic=Topic.find(params[:id])
  @topic.update_attributes(:istop => 0 )
  flash[:notice] = '解除置顶成功...'
  redirect_to :action => 'list' #,:layout => false
#  render :action => "list", :layout => false
  end

  def show
    @topic = Topic.find(params[:id])

  end

  def new
    @app_theme = Configuration.find_by_name("theme")
    @tags = Tag.find(:all)
    @topic = Topic.new

  end

  def create
    @topic = Topic.new(params[:topic])
    if  @topic.tag_id == 0
    @tag = Tag.new
    @tag.title = params[:tag_title]
    @tag.save
    @topic.tag_id = @tag.id
    else
    
    end
    @topic.user_id = session[:user_id]
    @topic.remote_ip=@request.remote_ip
    @attachment = SingleFile.save(@topic) if !params[:topic][:attachment_id].blank?
    @topic.attachment_id = @attachment.id if @attachment
    #@topic.user_id =
    if @topic.save
      @attachment.update_attributes(:topic_id => @topic.id ) if @attachment
      flash[:notice] = 'Topic was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @tags = Tag.find(:all)
    @topic = Topic.find(params[:id])
    @attachment = Attachment.find(@topic.attachment_id) if !@topic.attachment_id.blank?
  end

  def update
    @topic = Topic.find(params[:id])
    #@attachment = SingleFile.save(@topic) if !params[:topic][:attachment_id].blank?
    #@topic.attachment_id = @attachment.id if @attachment
    if @topic.update_attributes(params[:topic])
      flash[:notice] = 'Topic was successfully updated.'
      redirect_to :action => 'index'
    else
      render :action => 'edit'
    end
  end
  


  def destroy
    Topic.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
